<template>
  <div>
    <div class="container-fluid">
      <div class="spannel" v-for="(item,index) in titlelist" :key="index">
        <div class="left">
          <p>{{item.num}}</p>
          <span>{{item.title}}</span>
        </div>
        <div class="right">
          <div class="icon">
            <i :class="item.icon"></i>
          </div>
        </div>
      </div>
    </div>

    <div class="container-fluid2">
      <el-row class="curve-pie" >
        <el-col class="line" :sm="20" :xs="16">
          <div class="gragh_pannel" id="curve_show"></div>
        </el-col>

        <el-col class="pai" :sm="4" :xs="8">
          <div class="gragh_pannel" id="pie_show"></div>
        </el-col>
      </el-row>
    </div>

    <div class="container-fluid2">
      <div class="column_pannel" id="column_show"></div>
    </div>
  </div>
</template>

<script>
// 模块内用命名导出的, 我把他们再收集起来形成对象, 放到echrats变量里
import * as echarts from 'echarts'
export default {
  data () {
    return {
      titlelist: [{
        num: '10040',
        title: '文章总数',
        icon: 'el-icon-document-copy'
      }, {
        num: '1056',
        title: '日增文章数',
        icon: 'el-icon-document'
      }, {
        num: '11148',
        title: '评论总数',
        icon: 'el-icon-chat-dot-square'
      }, {
        num: '420',
        title: '日新增评论数',
        icon: 'el-icon-chat-line-square'
      }]
    }
  },
  name: 'my-home',
  mounted () {
    // 面积图
    this.setCurveChartFn()
    // 饼状图
    this.picChartFn()
    // 柱状图
    this.columnChartFn()

    // 窗口尺寸改变
    window.onresize = () => {
      return (() => {
        // this.$forceUpdate();//强制更新数据
        this.$router.go(0)
      })()
    }
  },
  methods: {
    // 设置面积图
    setCurveChartFn () {
      // 基于准备好的dom，初始化echarts实例
      const curveChart = echarts.init(document.getElementById('curve_show')) //, null, {        width: 1000,        height: 400      }
      // 绘制面积图表
      // 数据源(模拟后台返回的数据)
      const aListAll = [
        { count: 36, date: '2022-04-13' },
        { count: 52, date: '2022-04-14' },
        { count: 78, date: '2022-04-15' },
        { count: 85, date: '2022-04-16' },
        { count: 65, date: '2022-04-17' },
        { count: 72, date: '2022-04-18' },
        { count: 88, date: '2022-04-19' },
        { count: 64, date: '2022-04-20' },
        { count: 72, date: '2022-04-21' },
        { count: 90, date: '2022-04-22' },
        { count: 96, date: '2022-04-23' },
        { count: 100, date: '2022-04-24' },
        { count: 102, date: '2022-04-25' },
        { count: 110, date: '2022-04-26' },
        { count: 123, date: '2022-04-27' },
        { count: 100, date: '2022-04-28' },
        { count: 132, date: '2022-04-29' },
        { count: 146, date: '2022-04-30' },
        { count: 200, date: '2022-05-01' },
        { count: 180, date: '2022-05-02' },
        { count: 163, date: '2022-05-03' },
        { count: 110, date: '2022-05-04' },
        { count: 80, date: '2022-05-05' },
        { count: 82, date: '2022-05-06' },
        { count: 70, date: '2022-05-07' },
        { count: 65, date: '2022-05-08' },
        { count: 54, date: '2022-05-09' },
        { count: 40, date: '2022-05-10' },
        { count: 45, date: '2022-05-11' },
        { count: 38, date: '2022-05-12' }
      ]

      // 但是图标要求直接给数字的值, 所以要把对象的值取出来形成2个只有值的数组
      const aCount = []
      const aDate = []

      for (let i = 0; i < aListAll.length; i++) {
        aCount.push(aListAll[i].count)
        aDate.push(aListAll[i].date)
      }

      // 面积图的echarts配置项(参考文档复制)
      const chartopt = {
        title: {
          text: '月新增文章数', // 标题
          left: '30', // 位置居中
          top: '30' // 标题距离容器顶部px
        },
        tooltip: { // 提示框组件
          // trigger: 'axis' // 坐标轴触发(鼠标放在坐标轴就能出提示框)
        },

        toolbox: { // 工具栏
          show: true, // 要显示
          itemGap: 30,
          top: 20,
          right: 20,
          feature: { // 各工具配置项
            mark: { show: true }, // 辅助线开关
            dataView: { show: true, readOnly: false }, // 数据视图工具，可以展现当前图表所用的数据，编辑后可以动态更新。
            magicType: { show: true, type: ['line', 'bar'] }, // 动态类型切换
            restore: { show: true }, // 配置项还原
            saveAsImage: { show: true } // 点击保存下载图片
          }
        },
        xAxis: [ // 水平轴显示
          {
            type: 'category',
            boundaryGap: false, // 坐标轴两边留白策略 (不留白)
            data: aDate // 水平轴上的数字数据 (时间)

          }
        ],
        yAxis: [ // 垂直轴显示
          {
            type: 'value' // 以series里的data值做划分段
          }
        ],
        series: [ // 系列(控制图表类型和数据)
          {
            name: '新增文章',
            type: 'line', // 折线图
            smooth: true, // 是否平滑视觉引导线，默认不平滑，可以设置成 true 平滑显示
            // areaStyle: { type: 'default' }, // 区域填充样式。设置后显示成区域面积图。
            // itemStyle: { color: '#f80', lineStyle: { color: '#f80' } }, // 折线拐点标志的样式。
            itemStyle: {
              // color: '#f80',
              lineStyle: {
                color: '#f80'
              }
            }, // 折线拐点标志的样式。
            data: aCount, // 真正数据源(用下标和x轴数组对应)
            color: {
              x: 0,
              y: 0,
              x2: 1,
              y2: 0,
              colorStops: [
                {
                  offset: 0,
                  color: 'rgba(156, 235, 225, 0.4)' // 0% 处的颜色
                },
                {
                  offset: 0.5,
                  color: 'rgba(130, 175, 230,0.4)' // 50% 处的颜色
                },
                {
                  offset: 1,
                  color: 'rgba(102, 115, 234,0.4)' // 100% 处的颜色
                }
              ],
              globalCoord: false // 缺省为 false
            },
            areaStyle: {
              shadowBlur: 10,
              shadowColor: '#fff',
              shadowOffsetX: 10,
              shadowOffsetY: 10,
              opacity: 0.5,
              color: {
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [
                  {
                    offset: 0,
                    color: 'rgba(157, 236, 225, 0.4)' // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: 'rgba(130, 175, 230,0.4)' // 50% 处的颜色
                  },
                  {
                    offset: 1,
                    color: 'rgba(100, 115, 235,0.4)' // 100% 处的颜色
                  }
                ],
                globalCoord: false // 缺省为 false
              }
            }
          }
        ],
        grid: { // 直角坐标系内绘图网格
          show: false,
          width: 'auto',
          x: 50, // grid 组件离容器偏移量, 左侧
          x2: 50, // 右侧
          y: 100 // 上
          // height:  // 高度
        },
        symbol: 'arrow'
      }

      curveChart.setOption(chartopt)
    },
    // 设置饼状图
    picChartFn () {
      const myDate = [{ value: 300, name: '奇趣事' }, { value: 100, name: '会生活' }, { value: 260, name: '爱旅行' }, { value: 180, name: '趣美味' }]
      const oPie = echarts.init(document.getElementById('pie_show'))
      const oPieopt = {
        title: {
          top: 10,
          left: 50,
          text: '分类文章数量比'
        },
        tooltip: {
          trigger: 'item', // 在图形上才会触发提示
          formatter: '{a} <br/>{b} : {c} ({d}%)' // 提示的文字显示的格式
          // a: 系列名
          // b: 数据名
          // c: 数值
          // d: 百分比 (只有饼状图生效)
        },
        color: ['#5482fa', '#68deb3', '#7dbfff', '#586ee6'], // 系列颜色
        legend: { // 图例组件
          orient: 'vertical',
          bottom: 100,
          x: 'center',
          data: myDate, // 每个部分对应的数据名(要和series里name对应)
          formatter: function (name) {
            let total = 0
            let tarValue
            for (let i = 0; i < myDate.length; i++) {
              total += myDate[i].value
              if (myDate[i].name === name) {
                tarValue = myDate[i].value
              }
            }
            const p = Math.round((tarValue / total) * 100) + '%'
            return `${name}                           ${p}`
          }
        },
        toolbox: { // 工具栏
          show: true, // 要显示
          x: 'center',
          bottom: 20,
          itemGap: 30,
          right: 10,
          feature: { // 各工具配置项
            mark: { show: true }, // 辅助线开关
            dataView: { show: true, readOnly: false }, // 数据视图工具，可以展现当前图表所用的数据，编辑后可以动态更新。
            magicType: { show: true, type: ['line', 'bar'] }, // 动态类型切换
            restore: { show: true }, // 配置项还原
            saveAsImage: { show: true } // 点击保存下载图片
          }
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: ['30%', '48%'],
            center: ['50%', '30%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 0,
              borderColor: '#fff',
              borderWidth: 1
            },
            label: {
              show: true,
              position: 'center'
            },
            labelLine: {
              show: false
            },
            data: myDate
          }
        ]
      }
      oPie.setOption(oPieopt)
    },
    // 设置底部柱状图
    columnChartFn () {
      const oColumn = echarts.init(document.getElementById('column_show'))
      const oColumnopt = {
        title: {
          text: '文章访问量',
          left: 'center',
          top: '10'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['奇趣事', '会生活', '爱旅行', '趣美味'],
          top: '40'
        },
        toolbox: {
          itemGap: 30,
          right: 30,
          show: true,
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        xAxis: [
          {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月'],
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            }
          }
        ],
        yAxis: [
          {
            name: '访问量',
            type: 'value',
            splitLine: {
              show: true,
              lineStyle: {
                type: 'dashed'
              }
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            }
          }
        ],
        series: [
          {
            name: '奇趣事',
            type: 'bar',
            barWidth: 20,
            areaStyle: { type: 'default' },
            itemStyle: {
              color: '#5482fa',
              barBorderRadius: [5, 5, 5, 5]
            },
            data: [800, 708, 920, 1090, 1200]
          },
          {
            name: '会生活',
            type: 'bar',
            barWidth: 20,
            areaStyle: { type: 'default' },
            itemStyle: {
              normal: {
                // 这里设置柱形图圆角 [左上角，右上角，右下角，左下角]
                barBorderRadius: [5, 5, 5, 5],
                color: '#68deb3'
              }
            },
            data: [400, 468, 520, 690, 800]
          },
          {
            name: '爱旅行',
            type: 'bar',
            barWidth: 20,
            areaStyle: { type: 'default' },
            itemStyle: {
              barBorderRadius: [5, 5, 5, 5],
              color: '#7dbfff'
            },
            data: [500, 668, 520, 790, 900]
          },
          {
            name: '趣美味',
            type: 'bar',
            barWidth: 20,
            areaStyle: { type: 'default' },
            itemStyle: {
              barBorderRadius: [5, 5, 5, 5],
              color: '#586ee6'
            },
            data: [600, 508, 720, 890, 1000]
          }
        ],
        grid: {
          show: true,
          x: 50,
          x2: 30,
          y: 80

        }

      }
      oColumn.setOption(oColumnopt)
    }
  }
}
</script>

<style lang="less" scoped>
.container-fluid {
  width: 100%;
  display: flex;
  background-color: #fff;
  margin-bottom: 1rem;
}
.spannel {

  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 2rem 0 2rem 2rem;
  padding-right: 2rem;

  border-right: 1px solid #6666;
}
.spannel:last-child {
  border: none;
}
.left,.right {
  margin: 1rem 1rem;
}

.icon {
  width: 6rem;
  height: 6rem;
  background-color: #F3F7FE;
  border-radius: 50%;
  text-align: center;
  line-height: 7rem;
}
p {
  margin:0 0 0.5rem 0;
  font-size: 28px;
  font-weight: 550;
}
span {
  font-size: 15px;
  color: #5A607F;
}
i {
  font-size: 3rem;
  color: #3E94FF;
}

.gragh_pannel {
  height: 39rem;
  border: 1px solid #e7e7e9;
  background-color: #fff !important;
  margin-bottom: 1rem;
}

.column_pannel {
  margin-bottom: 1rem;
  height: 30rem;
  width: 100%;
  border: 1px solid #e7e7e9;
  background-color: #fff !important;
}
.line {
  width: 78%;
  height: 100%;
}
.pai {
  width: 20%;
  margin-left: 2%;
}
</style>
